// mircoApp/activityClock/clockIndex/clockIndex.js
const app = getApp();
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    active:'',
    query:{
      status:'',
      title:'',
      page:1,
    },
    totalPage:0,
    isLoadMore:true,
    clockTab:[
      {
        name:'全部',
        val:0
      },
      {
        name:'进行中',
        val:2
      },
      {
        name:'未开始',
        val:1
      },
      {
        name:'已结束',
        val:3
      }
    ],
    clockList:[],
    info:{},
    iPnoneBottomBol:false,
    imgPath:app.globalData.imgPath
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getiPnoneBottom();
    this.initData();
    this.clockListData();
  },
  onSearch(event) {
    this.setData({
      'query.title':event.detail,
      'query.page':1,
      isLoadMore:true,
      clockList:[]
    })
    this.clockListData();
  },
  searchTap() {
    this.setData({
      'query.page':1,
      isLoadMore:true,
      clockList:[]
    })
    this.clockListData();
  },
  searchChange(event) {
    this.setData({
      'query.title':event.detail
    });
  },
  clockTabOn(event) {
    let {name} = event.detail;
    this.setData({
      'query.page':1,
      'query.status':name,
      clockList:[],
    })
    this.clockListData();
  },
  initData() {
    this.data.query.page = 1;
    this.clockList = [];
  },
  clockListData() {
    let urlStr = app.getNetAddresss("plugin.check-in-rebate.frontend.index.get-base-info");
    app._postNetWork({
      url: urlStr,
      data:{search:this.data.query},
      success:  (resdata) => {
        let res = resdata.data;
        let data = res.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          let {activity,set} = data;
          this.data.totalPage = activity.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            clockList: activity.data,
            info:set
          })
          wx.setNavigationBarTitle({
            title: set.plugin_name,
          })
        } else {
          this.data.query.page = this.data.query.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
   //获取更多数据
   getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.query.page >= this.data.totalPage) {
      return;
    } else {
      this.data.query.page = this.data.query.page + 1;
      let urlStr = app.getNetAddresss("plugin.check-in-rebate.frontend.index.get-base-info");
      app._postNetWork({
        url: urlStr,
        data: this.data.query,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data;
          let {activity,set} = list;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              clockList: this.data.clockList.concat(activity.data),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  getiPnoneBottom() {
    let that = this;
    try {
      let value = wx.getStorageSync('iPhoneBottom');
      if (value) {
        that.setData({
          iPnoneBottomBol: true
        });
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
     title: '刷新中...',
   })
   this.setData({
    'query.page':1,
      'query.status':'',
      isLoadMore:true,
      clockList:[],
    })
   this.clockListData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: info.plugin_name,
      path: "mircoApp/activityClock/clockIndex/clockIndex",
    };
  }
})